package com.example.demo.controller;

import com.example.demo.model.JwtResponse;
import com.example.demo.model.CustomUserDetails;
import com.example.demo.model.User;
import com.example.demo.repo.UserRepository;
import com.example.demo.utils.JwtTokenProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class AuthController {

    @Autowired
    AuthenticationManager authenticationManager;
    @Autowired
    UserRepository userRepository;
    @Autowired
    JwtTokenProvider tokenProvider;



    @Value("${app.jwtExpirationInMs}")
    private Long jwtExpirationInMs;
    @Value("${app.jwtExpirationRefreshInMs}")
    private Long jwtRefreshExpiration;

    @PostMapping("/loginSuccess")
    public ResponseEntity<?> authenticateUser(Authentication authentication) {

        CustomUserDetails customUserDetails = (CustomUserDetails) authentication;

        String accessToken = tokenProvider.generatorJwtToken(customUserDetails.getUsername(), "username", jwtExpirationInMs);
        String refreshToken = tokenProvider.generatorJwtToken(customUserDetails.getUsername(), "username", jwtRefreshExpiration);

        return ResponseEntity.ok(new JwtResponse(accessToken, refreshToken));
    }

    @PostMapping("/info")
    public String getInfo(HttpServletRequest request, User user) {
        String username = request.getParameter("username");
        System.out.println(username);
        return "ok";
    }

}
